package com.yinhe.module.ls.controller;

import com.yinhe.common.annotation.Log;
import com.yinhe.common.core.controller.BaseController;
import com.yinhe.common.core.domain.AjaxResult;
import com.yinhe.common.core.page.TableDataInfo;
import com.yinhe.common.enums.BusinessType;
import com.yinhe.common.utils.poi.ExcelUtil;
import com.yinhe.system.domain.YhEnterprise;
import com.yinhe.system.domain.YhEnterpriseLeader;
import com.yinhe.system.service.IYhEnterpriseLeaderService;
import com.yinhe.system.service.IYhEnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 承制单位信息Controller
 * 
 * @author yinhe
 * @date 2022-07-13
 */
@RestController
@RequestMapping("/ls/enterprise")
public class LsEnterpriseController extends BaseController
{
    @Autowired
    private IYhEnterpriseService yhEnterpriseService;
    @Autowired
    private IYhEnterpriseLeaderService yhEnterpriseLeaderService;

    /**
     * 查询承制单位信息列表
     */
    @PreAuthorize("@ss.hasPermi('module:ls:enterprise:list')")
    @GetMapping("/list")
    public TableDataInfo list(YhEnterprise yhEnterprise)
    {
        startPage();
        List<YhEnterprise> list = yhEnterpriseService.selectYhEnterpriseList(yhEnterprise);
        return getDataTable(list);
    }

    /**
     * 导出承制单位信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:enterprise:export')")
    @Log(title = "承制单位信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhEnterprise yhEnterprise)
    {
        List<YhEnterprise> list = yhEnterpriseService.selectYhEnterpriseList(yhEnterprise);
        ExcelUtil<YhEnterprise> util = new ExcelUtil<YhEnterprise>(YhEnterprise.class);
        util.exportExcel(response, list, "承制单位信息数据");
    }

    /**
     * 获取承制单位信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:enterprise:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id , HttpServletRequest request)
    {
        YhEnterprise yhEnterprise = yhEnterpriseService.selectYhEnterpriseById(id);
        request.getServletContext().setAttribute("id",id);
        // 获取领导列表
        YhEnterpriseLeader yhEnterpriseLeader = new YhEnterpriseLeader();
        yhEnterpriseLeader.setManufacturerId(id);
        List<YhEnterpriseLeader> list = yhEnterpriseLeaderService.selectYhEnterpriseLeaderList(yhEnterpriseLeader);
        // 插入
        yhEnterprise.setLeader(list);
       // System.out.println(list+"788");
        return AjaxResult.success(yhEnterprise);
    }
    @RequestMapping("/leader/{id}")
    public TableDataInfo list(HttpServletRequest request,@PathVariable Long id){
        System.out.println(id+"我是id");
        List<YhEnterpriseLeader> leader =  yhEnterpriseLeaderService.selectLeaderById(id);
        System.out.println(leader.size()+"888");
        return getDataTable(leader);
    }

    /**
     * 新增承制单位信息
     */
    @PreAuthorize("@ss.hasPermi('system:enterprise:add')")
    @Log(title = "承制单位信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhEnterprise yhEnterprise)
    {
        return toAjax(yhEnterpriseService.insertYhEnterprise(yhEnterprise));
    }

    /**
     * 新增领导信息
     */
//    @PreAuthorize("@ss.hasPermi('system:leader:add')")
//    @Log(title = "承制单位信息", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult addLeader(@RequestBody YhEnterpriseLeader yhEnterpriseLeader,
//                                @PathVariable("id") Long id)
//    {
//        YhEnterprise yhEnterprise = yhEnterpriseService.selectYhEnterpriseById(id);
//        yhEnterpriseLeader.setCreditCodes(yhEnterprise.getCreditCodes());
//        yhEnterpriseLeader.setManufacturerId(id);
//        return toAjax(yhEnterpriseLeaderService.insertYhEnterpriseLeader(yhEnterpriseLeader));
//    }

    /**
     * 修改承制单位信息
     */
    @PreAuthorize("@ss.hasPermi('system:enterprise:edit')")
    @Log(title = "承制单位信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhEnterprise yhEnterprise)
    {
        return toAjax(yhEnterpriseService.updateYhEnterprise(yhEnterprise));
    }

    /**
     * 删除承制单位信息
     */
    @PreAuthorize("@ss.hasPermi('system:enterprise:remove')")
    @Log(title = "承制单位信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yhEnterpriseService.deleteYhEnterpriseByIds(ids));
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        System.out.println("=============come==============");
        ExcelUtil<YhEnterprise> util = new ExcelUtil<YhEnterprise>(YhEnterprise.class);
        util.importTemplateExcel(response, "用户数据");
    }

    @Log(title = "信息导入", businessType = BusinessType.IMPORT)
   @PreAuthorize("@ss.hasPermi('system:enterprise:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {

        ExcelUtil<YhEnterprise> util = new ExcelUtil<YhEnterprise>(YhEnterprise.class);
        List<YhEnterprise> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = yhEnterpriseService.importEnterprise(userList, updateSupport,operName);
        return AjaxResult.success(message);
    }


}
